<?php

namespace app\controllers;
use yii\db\Query;
use yii\web\Controller;


class ApiCollectionController extends ApiBaseController {
     //收藏
    private $user_id;
    public function init(){
      $this->user_id=$this->GetUser()['user_id'];
    }

    //收藏列表
    public function actionIndex(){
        $where['user_id']=$this->user_id;
        $list=(new Query())->select('id,user_id,work_id')->from('yii_collection')->where($where)->all();
        foreach ($list as $k=>$v){
        $list[$k]['work']=$this->GetWork($v['work_id']);
        }
        return json_encode($list);

    }

    //岗位信息
    public function GetWork($work_id){
        $list=(new Query())->select('id,name,city,on,region,company,experience,education,min_salary,max_salary,welfare')->from('yii_work')->where(['id'=>$work_id])->one();
        $list['city']=$this->GetRegion($list['city']);
        $list['region']=$this->GetRegion($list['region']);
        $list['experience']=$this->GetExperience($list['experience']);
        $list['education']=$this->GetEducation($list['education']);
        $list['welfare']=$this->GetWelfare($list['welfare']);
        $list['company']=$this->GetCompany($list['company']);
        $list['collection']=10;
        return $list;
    }


    //取消收藏
    public function actionDel(){
      $where['work_id']=\Yii::$app->request->post()['work_id'];
      $where['user_id']=$this->user_id;
      $res=\Yii::$app->db->createCommand()->delete('yii_collection',$where)->execute();
        if ($res){
            return json_encode(['code'=>1,'msg'=>'取消成功']);
        }else{
            return json_encode(['code'=>0,'msg'=>'取消失败']);
        }

    }


    //收藏操作
    public function actionAdd(){
      $data['user_id']=$this->user_id;
      $data['work_id']=\Yii::$app->request->post()['work_id'];
      $check=(new Query())->select('*')->from('yii_collection')->where($data)->one();
      if ($check){
         return json_encode(['code'=>0,'msg'=>'你已经收藏']);
       }
      $data['create_time']=time();
      $res=\Yii::$app->db->createCommand()->insert('yii_collection',$data)->execute();
      if ($res){
          return json_encode(['code'=>1,'msg'=>'收藏成功']);
      }else{
          return json_encode(['code'=>0,'msg'=>'收藏失败']);
      }
    }


}
